package com.tjsinfo.tjsoa.worklog.dao;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateCallback;
import com.tjsinfo.tjsoa.worklog.vo.WorkLogMain;


public class WorkLogMainDao extends HibernateBaseDao implements IWorkLogMainDao{

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.worklog.hibernatedao.IWorkLogMainDao#selectOneById(java.lang.Integer)
	 */
	public WorkLogMain selectOneById(String id) {
		// TODO Auto-generated method stub
		return (WorkLogMain) getHibernateTemplate().get(WorkLogMain.class, id);
	}

	
	/**
	 * 根据类型和年份用户查询
	 * @param 工作日志
	 * @return
	 */
	public List selectWorkLogMainYearAndTypeAndUser(final WorkLogMain workLogMain) {
		// TODO Auto-generated method stub

		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkLogMain work where work.year = ? and work.type = ? and work.tjsUser.id = ? ");
						query.setParameter(0, workLogMain.getYear());
						query.setParameter(1, workLogMain.getType());	
						query.setParameter(2, workLogMain.getTjsUser().getUserId());
						return query.list();
					}
					
				});
	}
	
	/**
	 * 查询默认日日志
	 * @param 工作日志
	 * @return
	 */
	public List selectDefanultDateWorkLog(final WorkLogMain workLogMain) {
		// TODO Auto-generated method stub

		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkLogMain work where work.year = ? and work.type = ? and work.tjsUser.id = ?");
						query.setParameter(0, workLogMain.getYear());
						query.setParameter(1, workLogMain.getType());	
						query.setParameter(2, workLogMain.getTjsUser().getUserId());
						
						return query.list();
					}
					
				});
	}
	
	/**
	 * 根据类型和年份查询
	 * @param 工作日志
	 * @return
	 */
	public List selectWorkLogMainYearAndType(final WorkLogMain workLogMain) {
		// TODO Auto-generated method stub

		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkLogMain work where work.year = ? and work.type = ? ");
						query.setParameter(0, workLogMain.getYear());
						query.setParameter(1, workLogMain.getType());	
						return query.list();
					}
					
				});
	}

	/**
	 * 根据类型年份和状态查询
	 * @param 工作日志
	 * @return
	 */
	public List selectWorkLogMainYearAndTypeAndStart(final WorkLogMain workLogMain) {
		// TODO Auto-generated method stub
		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkLogMain work where work.year = ? and work.type = ? and work.start = ?");
						query.setParameter(0, workLogMain.getYear());
						query.setParameter(1, workLogMain.getType());
						query.setParameter(2, workLogMain.getStart());	
						return query.list();
					}
					
				});

	}

	/**
	 * 根据计划ID查询同时抓取批示
	 * @param 工作日志
	 * @return
	 */
	public WorkLogMain selectWorkLog( String jihua) {
		WorkLogMain workLogMain = null;
		/*return (WorkLogMain)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkLogMain work inner join  fetch work.workLogExamines where work.id = ?");
						query.setParameter(0, jihua);	
						return query.uniqueResult();
					}
					
				});*/
		Session session = getSession();
		Transaction transaction = 	session.beginTransaction();
		Query query =	session.createQuery("from  WorkLogMain work left join  fetch work.workLogExamines where work.id = ?");
		query.setParameter(0, jihua);
		workLogMain = (WorkLogMain) query.uniqueResult();
		transaction.commit();
		session.close();
		return workLogMain;
	}

	public WorkLogMain selectDateTime(final Date date, final String type, final Integer userid) {
		// TODO Auto-generated method stub
	
		return (WorkLogMain)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkLogMain work where work.time = ? and work.type = ? and work.tjsUser.id = ? ");
						query.setParameter(0, date);
						query.setParameter(1, type);
						query.setParameter(2, userid);
						return query.uniqueResult();
					}
					
				});
		
	}

	public List selectWorkLogMainUserAndType(final WorkLogMain workLogMain) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkLogMain work where work.tjsUser.id = ? and work.type = ? ");
						query.setParameter(0, workLogMain.getTjsUser().getUserId());
						query.setParameter(1, workLogMain.getType());
						return query.list();
					}
					
				});
	}

	

	public List selectWorkLogMainUserAndTypeAndStart(final WorkLogMain workLogMain) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkLogMain work where work.tjsUser.id = ? and work.type = ? ");
						query.setParameter(0, workLogMain.getTjsUser().getUserId());
						query.setParameter(1, workLogMain.getType());
						return query.list();
					}
					
				});
	}

	public List checkDefaultWorkLogYear(final WorkLogMain workLogMain) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkLogMain work where work.tjsUser.id = ? and work.type = ? ");
						query.setParameter(0, workLogMain.getTjsUser().getUserId());
						query.setParameter(1, workLogMain.getType());
						// query.setParameter(2, workLogMain.getSubmittype());
						return query.list();
					}
					
				});
	}
	public List selectWorkLogMainUser(final WorkLogMain workLogMain) {
		// TODO Auto-generated method stub
		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkLogMain work where work.tjsUser.id = ? ");
						query.setParameter(0, workLogMain.getTjsUser().getUserId());
						
						return query.list();
					}
					
				});
	}	

	/**
	 * 工作日志模糊查询
	 */
	public List selectLikeWorkLog(final WorkLogMain workLogMain) {
		// TODO Auto-generated method stub
		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkLogMain work where work.tjsUser.id = ? and work.content like ?");
						query.setParameter(0, workLogMain.getTjsUser().getUserId());
						query.setParameter(1, "%"+workLogMain.getContent()+"%");
						return query.list();
					}
					
				});

	}

	/**
	 * 工作日志类型模糊查询
	 */
	public List selectLikeWorkLogType(final WorkLogMain workLogMain) {
		// TODO Auto-generated method stub
		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkLogMain work where work.tjsUser.id = ? and work.content like ? and work.type = ?");
						query.setParameter(0, workLogMain.getTjsUser().getUserId());
						query.setParameter(1, "%"+workLogMain.getContent()+"%");
						query.setParameter(2, workLogMain.getType());
						return query.list();
					}
					
				});

	}		
}
